package jh.com.jhappbf.assay;

import android.content.Intent;
import android.os.Bundle;
import androidx.core.content.ContextCompat;
import android.text.TextUtils;
import android.view.Gravity;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;

import com.ab.activity.AbActivity;
import com.ab.fragment.AbLoadDialogFragment;
import com.ab.util.AbDialogUtil;
import com.ab.view.titlebar.AbTitleBar;
import com.ab.view.wheel.AbWheelUtil;
import com.ab.view.wheel.AbWheelView;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Locale;

import jh.com.jhappbf.R;
import jh.com.jhappbf.model.SettingModel;
import jh.com.jhappbf.weight.query.QuerySettingField;
import jh.com.jhappbf.weight.query.SelectActivity;

public class AssayQuerySetting extends AbActivity implements OnClickListener {

    private TextView mTxtStartDate;
    private TextView mTxtStartTime;
    private TextView mTxtEndDate;
    private TextView mTxtEndTime;
    private TextView mCargoname;
    private TextView mPlateNumber;
    private TextView mTxtSender;
    private TextView mTxtReceiver;
    private TextView mSettingFields;
    private Button mBtnStartDate;
    private Button mBtnStartTime;
    private Button mBtnEndDate;
    private Button mBtnEndTime;
    private Button mBtnYear;
    private Button mBtnCargoname;
    private Button mBtnPlatenumber;
    private Button mBtnSender;
    private Button mBtnReceiver;
    private Button mBtnExportExcel;
    private View mWheelDate;
    private final int GETCARGONAME = 0X001;
    private final int GETPLATENUMBER = 0X002;
    private final int GETSENDER = 0X0003;
    private final int GETRECEIVER = 0X0004;
    private final int GETSITEPOINT = 0X0005;
    SettingModel mSettingModel;
    private AbLoadDialogFragment mDialogFragment = null; //进度框


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setAbContentView(R.layout.query_assay_setting);

        AbTitleBar mAbTitleBar = this.getTitleBar();
        mAbTitleBar.setTitleText("化验查询设置");
        mAbTitleBar.setLogo(R.drawable.button_selector_back);
        mAbTitleBar.setTitleBarBackgroundColor(ContextCompat.getColor(this, R.color.orange));
        mAbTitleBar.setTitleTextMargin(10, 0, 0, 0);
        mAbTitleBar.setLogoLine(R.mipmap.line);
        mAbTitleBar.setLogoOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {


                saveSettings();
            }
        });

        initView();
    }

    private void saveSettings() {
        String startDate = mTxtStartDate.getText().toString().trim();
        String startTime = mTxtStartTime.getText().toString().trim();
        String endDate = mTxtEndDate.getText().toString().trim();
        String endTime = mTxtEndTime.getText().toString().trim();
        String cargoname = mCargoname.getText().toString();
        String platenumber = mPlateNumber.getText().toString();
        String sender = mTxtSender.getText().toString();
        String receiver = mTxtReceiver.getText().toString();
        if ((startDate.length() > 0 && startTime.length() > 0) ||
                (endDate.length() > 0 && endTime.length() > 0) ||
                cargoname.length() > 0 ||
                platenumber.length() > 0) {
            mSettingModel = new SettingModel();
            if (startDate.length() > 0 && startTime.length() > 0) {
                mSettingModel.setStartDateTime(startDate + " " + startTime);
            }

            if (endDate.length() > 0 && endTime.length() > 0) {
                mSettingModel.setEndDateTime(endDate + " " + endTime);
            }
            mSettingModel.setPlatenumber(platenumber);
            mSettingModel.setCargoname(cargoname);
            mSettingModel.setSender(sender);
            mSettingModel.setReceiver(receiver);
            mSettingModel.setSitePoint("");
        } else {
            mSettingModel = null;
        }
        Intent i = new Intent();
        i.putExtra("result", mSettingModel);
        setResult(1, i);
        AssayQuerySetting.this.finish();
    }

    /**
     * 初始化
     */
    private void initView() {
        mTxtStartDate = findViewById(R.id.query_weight_setting_start_date_txt);
        mTxtStartTime = findViewById(R.id.query_weight_setting_start_time_txt);
        mTxtEndDate = findViewById(R.id.query_weight_setting_end_date_txt);
        mTxtEndTime = findViewById(R.id.query_weight_setting_end_time_txt);
        mCargoname = findViewById(R.id.query_weight_setting_cargoname_txt);
        mTxtSender = findViewById(R.id.query_weight_setting_sender_txt);
        mTxtReceiver = findViewById(R.id.query_weight_setting_receiver_txt);
        mPlateNumber = findViewById(R.id.query_weight_setting_plateNumber_txt);
        mBtnStartDate = findViewById(R.id.query_weight_setting_start_date_btn);
        mBtnStartTime = findViewById(R.id.query_weight_setting_start_time_btn);
        mBtnEndDate = findViewById(R.id.query_weight_setting_end_date_btn);
        mBtnEndTime = findViewById(R.id.query_weight_setting_end_time_btn);
        mBtnCargoname = findViewById(R.id.query_weight_setting_cargoname_btn);
        mBtnPlatenumber = findViewById(R.id.query_weight_setting_platenumber_btn);
        mBtnReceiver = findViewById(R.id.query_weight_setting_receiver_btn);
        mBtnSender = findViewById(R.id.query_weight_setting_sender_btn);
        mBtnExportExcel = findViewById(R.id.query_btn_exportExcel);
        mSettingFields = findViewById(R.id.query_weight_setting_field_txt);

        mTxtStartDate.setOnClickListener(this);
        mTxtStartTime.setOnClickListener(this);
        mTxtEndDate.setOnClickListener(this);
        mTxtEndTime.setOnClickListener(this);
        mTxtSender.setOnClickListener(this);
        mTxtReceiver.setOnClickListener(this);
        mCargoname.setOnClickListener(this);
        mPlateNumber.setOnClickListener(this);
        mSettingFields.setOnClickListener(this);

        mBtnStartDate.setOnClickListener(this);
        mBtnStartTime.setOnClickListener(this);
        mBtnEndDate.setOnClickListener(this);
        mBtnEndTime.setOnClickListener(this);
        mBtnCargoname.setOnClickListener(this);
        mBtnPlatenumber.setOnClickListener(this);
        mBtnReceiver.setOnClickListener(this);
        mBtnSender.setOnClickListener(this);
        mBtnExportExcel.setOnClickListener(this);


        Intent intent = getIntent();
        SettingModel mode = intent.getParcelableExtra("data");
        if (mode != null) {
            String start = mode.getStartDateTime().trim();
            String end = mode.getEndDateTime().trim();
            if (!TextUtils.isEmpty(start)) {
                String[] arr = start.split(" ");
                if (arr.length > 1) {
                    mTxtStartDate.setText(arr[0]);
                    mTxtStartTime.setText(arr[1]);
                }
            }

            if (!TextUtils.isEmpty(end)) {
                String[] arr = end.split(" ");
                if (arr.length > 1) {
                    mTxtEndDate.setText(arr[0]);
                    mTxtEndTime.setText(arr[1]);
                }
            }
            String cargoname = mode.getCargoname();
            String platenumber = mode.getPlatenumber();
            String sender = mode.getSender();
            String receiver = mode.getReceiver();
//            String sitePoint = mode.getSitePoint();
            if (cargoname != null) {
                mCargoname.setText(cargoname);
            }

            if (platenumber != null) {
                mPlateNumber.setText(platenumber);
            }

            if (sender != null) {
                mTxtSender.setText(sender);
            }

            if (receiver != null) {
                mTxtReceiver.setText(receiver);
            }

        } else {
            SimpleDateFormat formatDate = new SimpleDateFormat("yyyy-MM-dd", Locale.getDefault());
            SimpleDateFormat formatTime = new SimpleDateFormat("HH:mm", Locale.getDefault());
            Calendar calendar = Calendar.getInstance();
            mTxtEndDate.setText(formatDate.format(calendar.getTime()));
            mTxtEndTime.setText("23:59");

//			calendar.add(Calendar.DAY_OF_MONTH, -1);
            mTxtStartDate.setText(formatDate.format(calendar.getTime()));
            mTxtStartTime.setText("00:00");

        }
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.query_weight_setting_start_date_txt:
                initWheelThree(v, true);
                break;
            case R.id.query_weight_setting_start_time_txt:
                initWheelThreeTime(v);
                break;
            case R.id.query_weight_setting_end_date_txt:
                initWheelThree(v, false);
                break;
            case R.id.query_weight_setting_end_time_txt:
                initWheelThreeTime(v);
                break;
            case R.id.query_weight_setting_cargoname_txt:
                getFieldVal("cargoname", GETCARGONAME);
                break;
            case R.id.query_weight_setting_plateNumber_txt:
                getFieldVal("platenumber", GETPLATENUMBER);
                break;

            case R.id.query_weight_setting_sender_txt:
                getFieldVal("sender", GETSENDER);
                break;
            case R.id.query_weight_setting_receiver_txt:
                getFieldVal("receiver", GETRECEIVER);
                break;
            case R.id.query_weight_setting_start_date_btn:
                mTxtStartDate.setText("");
                break;
            case R.id.query_weight_setting_start_time_btn:
                mTxtStartTime.setText("");
                break;
            case R.id.query_weight_setting_end_date_btn:
                mTxtEndDate.setText("");
                break;
            case R.id.query_weight_setting_end_time_btn:
                mTxtEndTime.setText("");
                break;
            case R.id.query_weight_setting_cargoname_btn:
                mCargoname.setText("");
                break;
            case R.id.query_weight_setting_platenumber_btn:
                mPlateNumber.setText("");
                break;
            case R.id.query_weight_setting_sender_btn:
                mTxtSender.setText("");
                break;
            case R.id.query_weight_setting_receiver_btn:
                mTxtReceiver.setText("");
                break;
            case R.id.query_weight_setting_field_txt:
                Intent i = new Intent(AssayQuerySetting.this, QuerySettingField.class);
                startActivity(i);
            case R.id.query_btn_exportExcel:
//			export();
                break;
            default:
                break;
        }
    }


    /**
     * 设置日期
     */
    private void initWheelThree(View txt, boolean isStart) {
        mWheelDate = mInflater.inflate(R.layout.wheel_three_activity, null);
        initWheelDate(mWheelDate, (TextView) txt, "yyyy-MM-dd", isStart);
        AbDialogUtil.showDialog(mWheelDate, Gravity.BOTTOM);
    }

    /**
     * 设置时间
     */
    private void initWheelThreeTime(View txtTime) {
        mWheelDate = mInflater.inflate(R.layout.wheel_three_activity, null);
        initWheelTime(mWheelDate, (TextView) txtTime);
        AbDialogUtil.showDialog(mWheelDate, Gravity.BOTTOM);
    }

    public void initWheelDate(View mDateView, TextView mText, String flag, boolean isStart) {
        Calendar calendar = Calendar.getInstance();
        int year = calendar.get(Calendar.YEAR);
        int month = calendar.get(Calendar.MONTH) + 1;
        int day = calendar.get(Calendar.DATE);
        if (isStart) {
            day = day - 1;
        }
        final AbWheelView mWheelViewY = mDateView.findViewById(R.id.wheelView1);
        final AbWheelView mWheelViewM = mDateView.findViewById(R.id.wheelView2);
        final AbWheelView mWheelViewD = mDateView.findViewById(R.id.wheelView3);
        if ("yyyy-MM".equals(flag)) {
            mWheelViewD.setVisibility(View.GONE);
        } else if ("yyyy".equals(flag)) {
            mWheelViewD.setVisibility(View.GONE);
            mWheelViewM.setVisibility(View.GONE);
        }
        Button okBtn = mDateView.findViewById(R.id.okBtn);
        Button cancelBtn = mDateView.findViewById(R.id.cancelBtn);
        mWheelViewY.setCenterSelectDrawable(ContextCompat.getDrawable(this, R.drawable.wheel_select));
        mWheelViewM.setCenterSelectDrawable(ContextCompat.getDrawable(this, R.drawable.wheel_select));
        mWheelViewD.setCenterSelectDrawable(ContextCompat.getDrawable(this, R.drawable.wheel_select));
        AbWheelUtil.initWheelDatePicker(this, mText, mWheelViewY, mWheelViewM,
                mWheelViewD, okBtn, cancelBtn, year, month, day, 2000, 30,
                true, flag);
    }

    /**
     * 初始化时间
     */
    public void initWheelTime(View mDateView, TextView mText) {
        Calendar calendar = Calendar.getInstance();
        int hour = calendar.get(Calendar.HOUR);
        int minute = calendar.get(Calendar.MINUTE) + 1;
        final AbWheelView mWheelViewH = mDateView.findViewById(R.id.wheelView1);
        mWheelViewH.setVisibility(View.GONE);
        final AbWheelView mWheelViewM = mDateView.findViewById(R.id.wheelView2);
        final AbWheelView mWheelViewS = mDateView.findViewById(R.id.wheelView3);

        Button okBtn = mDateView.findViewById(R.id.okBtn);
        Button cancelBtn = mDateView.findViewById(R.id.cancelBtn);
        mWheelViewM.setCenterSelectDrawable(ContextCompat.getDrawable(this, R.drawable.wheel_select));
        mWheelViewS.setCenterSelectDrawable(ContextCompat.getDrawable(this, R.drawable.wheel_select));

        AbWheelUtil.initWheelTimePicker2(this, mText, mWheelViewM, mWheelViewS,
                okBtn, cancelBtn, hour, minute, true);
    }

    private void getFieldVal(String flag, int requestCode) {
        Intent i = new Intent(AssayQuerySetting.this, SelectActivity.class);
        i.putExtra("flag", flag);
        startActivityForResult(i, requestCode);
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {

        String result = "";
        if (data != null) {
            result = data.getStringExtra("result");
        }

        switch (requestCode) {
            case GETCARGONAME:
                mCargoname.setText(result);
                break;
            case GETPLATENUMBER:
                mPlateNumber.setText(result);
                break;
            case GETSENDER:
                mTxtSender.setText(result);
                break;
            case GETRECEIVER:
                mTxtReceiver.setText(result);
                break;
            default:
        }

    }

    @Override
    public void onBackPressed() {
        saveSettings();
    }
}